#include "HelloAppService.h"
#include "Closure.h"
#include <ConfigManager.h>

HelloAppServiceImpl::HelloAppServiceImpl() {
}

HelloAppServiceImpl::~HelloAppServiceImpl() {
}

void HelloAppServiceImpl::init()
{
    ConfigManager cfg;
    cfg.load("./conf/HelloApp/cfg.yml");

    auto timeUpdate = cfg.GetInt("config.HelloApp.time.update");
    auto host = cfg.GetString("config.HelloApp.url");
    if (!host.empty()) {
    }
}

void HelloAppServiceImpl::HelloSync(::google::protobuf::RpcController* controller,
                                const::HelloRequest* request, 
                                ::HelloResponse* response, ::google::protobuf::Closure* done)
{
    auto listValue = response->add_list();
    listValue->set_name("HelloSync");
    listValue->set_code("dummy");
}


void HelloAppServiceImpl::HelloAsync(::google::protobuf::RpcController* controller,
    const::HelloRequest* request,
    ::HelloResponse* response, ::google::protobuf::Closure* done)
{
    emoney::Closure* pClosure = (emoney::Closure*)done;
    if (pClosure != nullptr)
    {
        pClosure->SetAsync();

        std::async(std::launch::async,[this, request, response, pClosure]() {
            auto listValue = response->add_list();
            listValue->set_name("HelloAsync");
            listValue->set_code("dummy");
            pClosure->Run();
        });
    }
}

void HelloAppServiceImpl::HelloCustom(::google::protobuf::RpcController* controller,
    const::HelloRequest* request, ::HelloResponse* inorge, ::google::protobuf::Closure* done)
{
    emoney::Closure* pClosure = (emoney::Closure*)done;
    pClosure->SetCustomReply("HelloCustom");
}

void HelloAppServiceImpl::HelloError(::google::protobuf::RpcController* controller, const::HelloRequest* request, ::HelloResponse* response, ::google::protobuf::Closure* done)
{
    emoney::Closure* pClosure = (emoney::Closure*)done;
    pClosure->SetError(-1, "HelloError");
}
